<?php

namespace app\api\Logic\Course;

use app\api\model\CampusCourseMallGoodsModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class MallGoodsLogic
{
    /**
     * 选项课程详情
     * @param $shopId
     * @param $mallGoodsId
     * @return array
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function info($shopId,$mallGoodsId){
        $managementPackageSublevelLogic = new ManagementPackageSublevelLogic();

        $info = CampusCourseMallGoodsModel::where('shop_id',$shopId)
            ->where('id',$mallGoodsId)
            ->with([
                'managementInfo' => function ($query) {
                    $query->withField('id,name');
                },
                'managementPackageInfo' => function ($query) {
                    $query->withField('id,name,name');
                }
            ])
            ->find();
        $info = objToArray($info) ?? [];

        if($info['type'] == 2){
            //套餐
            $info['management_package_info']['goods_list'] = $managementPackageSublevelLogic->getSublevelByPackageId($info['management_package_info']['id']);
        }else if($info['type'] == 1){
            //专项课
            //按课时收费
            if($info['hour_status'] == 1){
                $info['hour'] = unserialize($info['hour']);
            }
            //按月收费
            if($info['monthly_status'] == 1){
                $info['monthly'] = unserialize($info['monthly']);
            }
            //按天收费
            if($info['per_diem_status'] == 1){
                $info['day'] = unserialize($info['day']);
            }
        }

        return $info;
    }
}
